package com.juyu.miliao.api.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.sf.json.JSONObject;

import java.util.List;

/**
 * 获取参数
 * @author wbf
 */
@RestController
@RequestMapping("/appkey")
public class AppkeyController {
	@Resource
	AppKeyService appkeyService;
	
	/**
	 * 获取收费照片金币转换钻石价格
	 * @param type 请求数据类型：1私照价格。2闪图照片价格
	 * @return
	 */
	@GetMapping("/{type}")
	public JSONObject getAppkeyByKey(HttpServletRequest request, @PathVariable("type") Integer type){
		CommonHead com = VerifyUtil.verifyCommonHead(request);
		JSONObject json = new JSONObject();
		String value ="";
		if(type ==1){//私照价格
			value = appkeyService.getAppKeyByKey("privatePhoto").getValue();
			json.put("key", "privatePhoto");
		}else if(type ==2){//闪图照片价格
			value = appkeyService.getAppKeyByKey("chatPhoto").getValue();
			json.put("key", "chatPhoto");
		}else if(type ==3){//礼物转换钻石比例
			value = appkeyService.getAppKeyByKey("giftRatio").getValue();
			json.put("key", "giftRatio");
		}else if(type ==4){//获取视频送金币列表
			value = appkeyService.getAppKeyByKey("videoSpend").getValue();
			json.put("key", "videoSpend");
		}else if(type ==5){//二维码图片地址
			String key = "QRcode_"+com.getAppPackageId();
			value = appkeyService.getAppKeyByKey(key).getValue();
			if(value==null || value.equals("") || value.equals("null")){
				value = appkeyService.getAppKeyByKey("QRcode").getValue();
			}
			json.put("key","QRcode");
		}else if(type == 6){//语音价格
			value = appkeyService.getAppKeyByKey("chatVoice").getValue();
			json.put("key", "chatVoice");
		}else if(type == 7){//悄悄话语音金币价格转换钻石的价格
            String chatVoice = appkeyService.getAppKeyByKey("chatVoice").getValue();
            String giftRatio = appkeyService.getAppKeyByKey("giftRatio").getValue();
            value =String.valueOf((long) (Double.parseDouble(chatVoice)*Double.valueOf(giftRatio)));
		}else if(type ==8){//回答问题价格
			String answerToQuestion = appkeyService.getAppKeyByKey("answerToQuestion").getValue();
			String giftRatio = appkeyService.getAppKeyByKey("giftRatio").getValue();
			value =String.valueOf((long) (Double.parseDouble(answerToQuestion)*Double.valueOf(giftRatio)));
		}
		json.put("value", value);
		return json;
	}

	/**
	 * 测试使用
	 * @param key
	 * @return
	 */
	@GetMapping("/key/{key}")
	public Appkey getAppkeyByKey(@PathVariable("key") String key){
		return appkeyService.getAppKeyByKey(key);
	}


	@GetMapping("/update/{val}")
	public int updateIsH5Buy(@PathVariable("val") String val){
		return appkeyService.updateIsH5Buy(val);
	}

	/**
	 * 1.5.1 合并appkey 根据key查询appkey对象内容
	 * 传需要查询的key 用"," 分隔
	 * @param key
	 * @return
	 */
	@GetMapping("/list/{key}")
	public List<Appkey> getAppKeyByKeyList(@PathVariable("key") String key){
		return appkeyService.getAppKeyByKeyList(key);
	}
}